﻿using Microsoft.OpenApi.Models;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace GCC.APIContainer.SwaggerHelper
{

    internal class AddGCCAuthTokenHeaderParameter : IOperationFilter
    {

        internal static string _authHeadKey = "";
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
            {
                operation.Parameters = new List<OpenApiParameter>();
            }
            var attrs = context.ApiDescription.CustomAttributes();
            foreach (var item in attrs)
            {
                // 如果 Attribute 是我们自定义的验证过滤器
                if (item.GetType().Name.Equals("GCCAuthAttribute"))
                {
                    operation.Parameters.Add(new OpenApiParameter()
                    {
                        In = ParameterLocation.Header,
                        Name = _authHeadKey,
                        Required = false
                    });
                }
            }
        }
    }
}
